package com.zero.qth.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zero.common.core.domain.page.PageDomain;
import com.zero.qth.controller.req.PaymentRefundReq;
import com.zero.qth.vo.PaymentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zero.common.annotation.log.Log;
import com.zero.common.core.basecontroller.BaseController;
import com.zero.common.core.domain.result.AjaxResult;
import com.zero.common.core.enums.BusinessType;
import com.zero.qth.domain.Payment;
import com.zero.qth.service.IPaymentService;
import com.zero.common.core.utils.poi.ExcelUtil;
import com.zero.common.core.domain.page.TableDataInfo;

/**
 * 流水表Controller
 * 
 * @author cai-xu-kun
 * @date 2025-08-28
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "流水表")
@RequestMapping("/qth/payment")
public class PaymentController extends BaseController {

    private final IPaymentService paymentService;

    @ApiOperation("微信退款")
    @PreAuthorize("@ss.hasPermi('qth:payment:refund')")
    @Log(title = "微信退款", businessType = BusinessType.EXPORT)
    @PostMapping("/refund/{orderId}")
    public AjaxResult refund(@RequestBody PaymentRefundReq paymentRefundReq) {
        System.out.println("接收的订单号：" + paymentRefundReq.getOrderId());
        return paymentService.refund(paymentRefundReq);
    }

    @ApiOperation("获取流水表详细信息")
    @PreAuthorize("@ss.hasPermi('qth:payment:query')")
    @GetMapping(value = "/order/{orderId}")
    public AjaxResult getByorderId(@PathVariable("orderId") String orderId) {
        List<PaymentVo> paymentList = paymentService.selectPaymentVoByOrderId(orderId);
        if (paymentList == null){
            paymentList = new ArrayList<>();
        }
        return AjaxResult.success(paymentList);
    }


/*===========================================================*/
    /**
     * 查询流水表分页
     */
    @ApiOperation("查询流水表分页")
    @PreAuthorize("@ss.hasPermi('qth:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Payment payment) {
        PageDomain pageDomain = this.startPage();
        Page<Payment> p = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<PaymentVo> page = paymentService.selectPaymentListPage(payment, p);
        return this.getDataTable(page);
    }

    /**
     * 导出流水表列表
     */
    @ApiOperation("导出流水表列表")
    @PreAuthorize("@ss.hasPermi('qth:payment:export')")
    @Log(title = "流水表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Payment payment) {
        List<PaymentVo> list = paymentService.selectPaymentList(payment);
        ExcelUtil<PaymentVo> util = new ExcelUtil<PaymentVo>(PaymentVo.class);
        util.exportExcel(response, list, "流水表数据");
    }

    /**
     * 获取流水表详细信息
     */
    @ApiOperation("获取流水表详细信息")
    @PreAuthorize("@ss.hasPermi('qth:payment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(paymentService.selectPaymentById(id));
    }

    /**
     * 新增流水表
     */
    @ApiOperation("新增流水表")
    @PreAuthorize("@ss.hasPermi('qth:payment:add')")
    @Log(title = "流水表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payment payment) {
        return toAjax(paymentService.insertPayment(payment));
    }

    /**
     * 修改流水表
     */
    @ApiOperation("修改流水表")
    @PreAuthorize("@ss.hasPermi('qth:payment:edit')")
    @Log(title = "流水表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payment payment) {
        return toAjax(paymentService.updatePayment(payment));
    }

    /**
     * 删除流水表
     */
    @ApiOperation("删除流水表")
    @PreAuthorize("@ss.hasPermi('qth:payment:remove')")
    @Log(title = "流水表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(paymentService.deletePaymentByIds(ids));
    }
}
